import json
import os
from openpyxl import load_workbook
from app import BASE_DIR


# 封装断言方法
def common_assert(resp, msg, code):
    assert msg in resp.json().get("msg")
    assert code == resp.json().get("code")


# 读取json文件返回列表元组
def open_data_json(filename, datadir='data'):  # join 加入 拼接

    # os.path 系统拼接
    filepath = os.path.join(BASE_DIR, datadir, filename)
    with open(filepath, 'r', encoding='utf-8') as f:  # 别名f
        json_data = json.load(f)  # //返回数据
        list_data = []
        # for i in jsjson_dataon_data:
        #     list_data.append(tuple(i.values()))
        return json_data


def read_xlsx_test_dict(filename, datadir='data', sheet_name='Sheet1'):
    """
    读取测试用例数据
    :param sheet_name: 工作表名称  默认 Sheet1
    :param datadir: 文件路径 默认data
    :param filename: Excel文件名
    :return: 返回一个包含测试用例的列表，每个测试用例是一个字典
    """
    cases = []

    try:
        filepath = os.path.join(BASE_DIR, datadir, filename)
        print(filepath)
        workbook = load_workbook(filepath)
        sheet = workbook[sheet_name]

        headers = [cell.value for cell in sheet[1]]

        for row in sheet.iter_rows(min_row=2, values_only=True):
            test_case = dict(zip(headers, row))
            cases.append(test_case)

        return cases

    except FileNotFoundError:
        print(f"File not found: {filename}")
        return None
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

if __name__ == '__main__':

    print(open_data_json("login_data.json"))
